package biz.ws.receive.client;

import api.common.IdKey;
import biz.ws.WsConfig;
import biz.ws.receive.MessageExecutor;
import biz.ws.ws.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @创建人 wsdc
 * @时间 2021/4/2
 * @描述 日志中心的日志
 * // TODO: 2021/4/4
 */
@Component
public class ConsoleMessageExecutor implements MessageExecutor {
    @Resource
    UserContainer userContainer;

    @Resource
    WsConfig wsConfig;

    @Resource
    MsgSender msgSender;

    @Resource
    MsgDataSource msgDataSource;

    @Resource
    IdKey idKey;

    @Override
    public void onMessage(MsgHolder context) {
        Message msg = context.getMessage();

        //  保存消息不同在这个线程里面 异步操作时最好的
        msgSender.send(context, new MsgSender.SendListener() {
            @Override
            public void beforePost() {
                //  保存消息
                msgDataSource.save(msg);
            }

            @Override
            public void success() {
                //  发送成功 标记状态
                msg.setStatus(WsCst.MsgStatus.SEND);
                msgDataSource.update(msg);
            }

            @Override
            public void failure() {
                //  发送失败 标记状态
                msg.setStatus(WsCst.MsgStatus.SEND_FAILURE);
                msgDataSource.update(msg);
            }
        });
    }

    @Override
    public String type() {
        return WsCst.MsgType.SEND_USER;
    }
}
